package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.combat.*;
import com.itheima.domain.combatVo.*;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class CombatManager {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer webServer;
    @Reference
    private UserInfoService userInfoService;
    @Reference
    private ReportService reportService;
    @Reference
    private SoulService soulService;
    @Reference
    private DimensionService dimensionService;
    @Reference
    private QuestionnaireService questionnaireService;
    @Reference
    private QuestionSoulService questionSoulService;
    @Reference
    private OptionService optionService;


    /**
     * 灵魂试卷的列表
     * @return
     */
    public List<SoulVo> findQuestionnaire() {

        Long loginUserId = UserHolder.get().getId();
        //先去查询试卷
        List<Questionnaire> questionnaires = questionnaireService.findQuestionnaire();
        //创建问卷的vo对象的集合
        List<SoulVo> soulVoList = new ArrayList<>();
        //判断问卷的集合是否为空
        if (CollectionUtil.isNotEmpty(questionnaires)) {
            //对问卷的集合进行遍历
            for (Questionnaire questionnaire : questionnaires) {
                //创建问卷的vo对象
                SoulVo soulVo = new SoulVo();
                Report report = reportService.findByUserIdOrQuestionnaireId(loginUserId, questionnaire.getId());
                if (report != null) {
                    soulVo.setReportId(report.getId().toString());
                    soulVo.setIsLock(0);
                } else {
                    List<Report> reportList = reportService.findByUserId(loginUserId);
                    if (CollectionUtil.isNotEmpty(reportList)) {
                        if ("初级".equals(questionnaire.getLevel()) || "中级".equals(questionnaire.getLevel())) {
                            soulVo.setIsLock(0);
                        } else {
                            soulVo.setIsLock(1);
                            for (Report report1 : reportList) {
                                Questionnaire questionnaire1 = questionnaireService.findId(report1.getQuestionnaireId());
                                if ("中级".equals(questionnaire1.getLevel()) || "高级".equals(questionnaire1.getLevel())) {
                                    soulVo.setIsLock(0);
                                }
                            }
                        }
                    } else {
                        if ("初级".equals(questionnaire.getLevel())) {
                            soulVo.setIsLock(0);
                        } else {
                            soulVo.setIsLock(1);
                        }
                    }
                }
                //创建试题的vo集合
                List<QuestionSoulVo> questionSoulVoList = new ArrayList<>();
                //获取试卷的id
                Integer questionnaireId = questionnaire.getId();
                //根据试卷的id查询试题
                List<QuestionSoul> questionSoulList = questionSoulService.findQuestionSoulById(questionnaireId);
                if (CollectionUtil.isNotEmpty(questionSoulList)) {
                    for (QuestionSoul questionSoul : questionSoulList) {
                        //创建试题的vo集合
                        QuestionSoulVo questionSoulVo = new QuestionSoulVo();
                        //获取当前的试题的id
                        Integer questionSoulId = questionSoul.getId();
                        //根据试题的id查找选项的所有
                        List<Option> optionList = optionService.findOptionById(questionSoulId);
                        //创建选项的vo对象集合
                        List<OptionVo> optionVoList = new ArrayList<>();
                        if (CollectionUtil.isNotEmpty(optionList)) {
                            for (Option option : optionList) {
                                //创建选项的vo对象
                                OptionVo optionVo = new OptionVo();
                                //封装vo对象
                                optionVo.setOption(option.getOptionContext());
                                optionVo.setId(option.getId().toString());
                                optionVoList.add(optionVo);
                            }
                        }
                        //封装问题的vo对象
                        questionSoulVo.setOptions(optionVoList);
                        //封装vo对象
                        BeanUtils.copyProperties(questionSoul, questionSoulVo);
                        questionSoulVo.setId(questionSoul.getId().toString());
                        questionSoulVoList.add(questionSoulVo);
                    }
                }
                BeanUtils.copyProperties(questionnaire,soulVo);
                soulVo.setQuestions(questionSoulVoList);
                soulVo.setId(questionnaire.getId().toString());
                //将vo对象添加到集合中
                soulVoList.add(soulVo);
            }
        }
        return soulVoList;
    }

    /**
     * 根据试题和选项的编号查询分数
     * @param questionId
     * @param optionId
     * @return
     */
    public Integer submitReport(String questionId, String optionId) {
       Option option = optionService.findScore(questionId, optionId);
        Integer score = option.getScore();
        return score;
    }

    /**
     * 保存报告
     * @param score
     * @param questionnaireId
     * @return
     */
    public String  saveReport(Integer score,Integer questionnaireId) {
        //获取当前登录用户对象
        Long loginUserId = UserHolder.get().getId();
        //根据分数查询出灵魂
        Soul soul = soulService.findSoul(score);
        //创建报告对象
        Report report = new Report();
        report.setScore(score);//分数
        report.setUserId(loginUserId);//操作人的Id
        report.setQuestionnaireId(questionnaireId);//试卷的id
        if (soul != null) {
        report.setConclusion(soul.getSoulContext());//鉴定的结果
        report.setCover(soul.getCover());//鉴定的图片
        }
        //根据id查询试卷
        Questionnaire questionnaire = questionnaireService.findById(questionnaireId);
        //保存报告
        Integer reportId = reportService.saveReport(report);

        return reportId.toString();
    }

    /**
     * 根据试题的id查询试卷的id
     * @param questionId
     * @return
     */
    public Integer findQuestionById(String questionId) {
         QuestionSoul questionSoul = questionSoulService.findQuestionById(questionId);
        return questionSoul.getQuestionnaireId();
    }

    /**
     * 查看报告
     * @param reportId
     * @return
     */
    public ReportVo findReport(Integer reportId) {
        //获取当前登陆人的Id
        Long loginUserId = UserHolder.get().getId();
        Report report = reportService.findReport(reportId, loginUserId);
        if (report != null) {
            //创建报告的vo对象
        ReportVo reportVo = new ReportVo();
        BeanUtils.copyProperties(report, reportVo);
        Soul soul = soulService.findSoul(report.getScore());
        List<Dimension> dimension = dimensionService.findDimension(soul.getId());
        reportVo.setDimensions(dimension);
        //获取问卷的id
        Integer questionnaireId = report.getQuestionnaireId();
        //获取性格
        String conclusion = report.getConclusion();

        //根据性格和问卷的ID查找性格
        List<Report> reportList = reportService.findByConclusionOrQuestionnaireId(conclusion, questionnaireId);
            if (CollectionUtil.isNotEmpty(reportList)) {
                for (Report report1 : reportList) {
                    //创建与我相似的集合
                    List<SimilarYou> similarYouList = new ArrayList<>();
                    if (report1 != null) {
                        //判断是不是当前的用户
                        if (Long.valueOf(report1.getUserId()) != loginUserId) {
                            //创建与我相似的对象
                            SimilarYou similarYou = new SimilarYou();
                            //查询用户的
                            UserInfo userInfo = userInfoService.findById(report1.getUserId());
                            BeanUtils.copyProperties(userInfo, similarYou);
                            similarYouList.add(similarYou);
                            reportVo.setSimilarYou(similarYouList);
                        }
                    }
                }
            }
        System.out.println(reportVo);
        return reportVo;
        }
        return null;

    }
}
